<?php
  function VoteMember_adminform_main (){
    VoteMember_permission();
    VoteMember_adminform_submit ();
  }

  function VoteMember_permission() {
    // Security check
    //we are allow for admin access level , see in config.php variable name ACCESS_COMMENT
    if (!SecurityUtil::checkPermission('VoteMember::', '::', ACCESS_COMMENT)) {
        LogUtil::registerPermissionError(pnModUrl('Users','user','loginscreen'));
    }
  }

  function VoteMember_adminform_submit()
  {
    VoteMember_permission();
    $forward =  FormUtil::getPassedValue ('forward', null);
    $ctrl =  FormUtil::getPassedValue ('ctrl', null);
    $form = FormUtil::getPassedValue ('form', null);
    $id = FormUtil::getPassedValue ('id', null);

    $is_array = FormUtil::getPassedValue ('array', false);
    if ($is_array){
      $is_array = true;
    }else{
      $is_array = false;
    }
    if (empty($ctrl)){
      if ($form[ctrl]){
        $ctrl = $form[ctrl];      
      }else{
        return 'ERROR VoteMember system can not find controller variable';
      }
    }

    //Forward page and select value
    $list_url = pnModURL('VoteMember', 'admin', 'list' , array('ctrl'   => $ctrl));

    if (isset($_POST['button_cancel']) || isset($_POST['button_cancel_x'])){
        pnRedirect($list_url);
        return true;
    }

    if (!($class = Loader::loadClassFromModule ('VoteMember', $ctrl , $is_array))){
        return LogUtil::registerError ('Unable to load class [$ctrl] ...');
    }
    $object = new $class ();  
    $object->getDataFromInput ('form',null,'POST');    
	

    if ($_POST['button_delete'] || $_POST['button_delete_x']){
        $object->delete ();
    }else{
        $object->save ();
    }
    
    
    if (method_exists($object,'genForward')){
      $forward_url = $object->genForward();
      if (!empty($forward_url)){
        pnRedirect($forward_url);
      }
    }else if (count($forward) > 0 && $forward[id]){
      $forward_url = generateUrl($forward);
      pnRedirect($forward_url);
    }else{
      pnRedirect($list_url);
    }
    return true;
  }



  function VoteMember_adminform_savelayer() {
    VoteMember_permission();
  	$ext  = FormUtil::getPassedValue ('ext', NULL , 'REQUEST');
  	    
    $uid = FormUtil::getPassedValue ('uid', null);  
    $uname = FormUtil::getPassedValue ('uname', null);          
    $accessArray['province_access'] = serialize(explode(',', $ext['province_access']));
    $accessArray['district_access'] = serialize(explode(',', $ext['district_access']));
    $accessArray['tambon_access']   = serialize(explode(',', $ext['tambon_access']));

    //Forward page and select value
    $list_url = pnModURL('VoteMember', 'admin', 'read' , array('ctrl' => 'Member', 'uid' => $uid, 'uname' => $uname));
		//echo $list_url; exit;
    
    //print_r($object); exit;
    
    if ($_POST['button_delete'] || $_POST['button_delete_x']){
        $object->delete ();
    }else{
    	$columnArray = array('uid');
    	$where = 	" layer_uid = ".$uid;
    	$memberArray = DBUtil::selectObject('votemember_layer', $where, $columnArray);
    	if($memberArray['uid']){
		    //$accessArray = $object->insertPostProcess();    	
		    $where = 	" layer_uid = ".$memberArray['uid'];
				DBUtil::updateObject($accessArray, 'votemember_layer', $where);
			} else{
        //$object->save();			       
				$obj = array(
											'uid'             => $uid,
											'uname'           => $uname,
											'province_access' => $accessArray['province_access'],											
											'district_access' => $accessArray['district_access'],											
											'tambon_access'   => $accessArray['tambon_access']											
						 		);
				
				// do the insert
				$result = DBUtil::insertObject($obj, 'votemember_layer');
    
			}
		}
		   
		   

    pnRedirect($list_url);
    
    return true;
  }
  

  /**
  * @param $urlarray  the array to generate contain key and value
  *                   ex.  array('modname'=>'VoteMember'
                                 'func'   =>'save'
                                 'type'   =>'admin');
  */
  function generateUrl($urlarray){
    //default value

    $forward =  FormUtil::getPassedValue ('forward', null);

    $modname = 'VoteMember';

    if($forward['func']){
      $func = $forward['func'];
    }else{
      $func = 'list';
    }

    if($forward['id']){
      $param[] = 'id='.$forward['id'];
    }

    $type = 'admin';

    $param = array();
    foreach($urlarray as $key => $value){
      if (strcmp(strtolower($key),'modname') === 0){
        $modname = $value;
      }else if (strcmp(strtolower($key),'func') === 0){
        $func = $value;
      }else if (strcmp(strtolower($key),'type') === 0){
        $type = $value;
      }else{
        $param[] = $key . '=' .$value;
      }
    }
    return 'index.php?module=$modname&func=$func&type=$type&' . implode('&',$param);
  }

?>